/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;

using Gtk;

namespace DCSharp.Gui
{
	public class PageWindow : Window
	{
		public event EventHandler<PageEventArgs> CurrentPageChanged;
		public event EventHandler<PageEventArgs> PageAdded;
		public event EventHandler<PageEventArgs> PageRemoved;

		private Box pageManagerBox;
		private PageManager pageManager;
		private PageStyle pageStyle;

		private Comparison<Page> sortFunc;
		private PageCategoryHandler categoryFunc;

		#region Constructors

		public PageWindow(string title) : this(title, PageStyle.Tabs)
		{
		}

		public PageWindow(string title, PageStyle pageStyle) : base(title)
		{
			this.pageStyle = pageStyle;

			pageManagerBox = new HBox();
			pageManagerBox.Show();
			Add(pageManagerBox);

			pageManager = PageManager.Create(pageStyle);
			pageManager.CurrentPageChanged += OnCurrentPageChanged;
			pageManager.PageAdded += OnPageAdded;
			pageManager.PageRemoved += OnPageRemoved;

			pageManager.Show();
			pageManagerBox.PackStart(pageManager, true, true, 0);
		}

		#endregion

		#region Properties

		public PageStyle PageStyle
		{
			get { return pageStyle; }
			set { SetPageStyle(value); }
		}

		public int PageCount
		{
			get { return pageManager.Count; }
		}

		public Page Current
		{
			get { return pageManager.Current; }
		}

		#endregion

		#region Methods

		public void Add(Page page)
		{
			pageManager.Add(page);
		}

		public void Remove(Page page)
		{
			pageManager.Remove(page);
		}

		public bool Contains(Page page)
		{
			return pageManager.Contains(page);
		}

		public void Present(Page page)
		{
			pageManager.Present(page);
		}

		public T Find<T>(Predicate<T> match) where T : Page
		{
			return pageManager.Find(match);
		}

		public void SetCategoryFunc(PageCategoryHandler handler)
		{
			categoryFunc = handler;

			PageManagerTree manager = pageManager as PageManagerTree;
			if (manager != null)
			{
				manager.CategoryFunc = categoryFunc;
			}
		}

		public void SetSortFunc(Comparison<Page> comparer)
		{
			sortFunc = comparer;

			PageManagerTree manager = pageManager as PageManagerTree;
			if (manager != null)
			{
				manager.SortFunc = sortFunc;
			}
		}

		public void Resort()
		{
			PageManagerTree manager = pageManager as PageManagerTree;
			if (manager != null)
			{
				manager.Resort();
			}
		}

		public override void Destroy()
		{
			pageManager.Clear();
			base.Destroy();
		}

		protected virtual void SetPageStyle(PageStyle pageStyle)
		{
			if (this.pageStyle == pageStyle)
			{
				return;
			}
			this.pageStyle = pageStyle;

			PageManager oldManager = pageManager;
			pageManagerBox.Remove(oldManager);

			pageManager = PageManager.Create(pageStyle);
			pageManagerBox.PackStart(pageManager, true, true, 0);
			pageManager.Show();

			PageManagerTree manager = pageManager as PageManagerTree;
			if (manager != null)
			{
				manager.SortFunc = sortFunc;
				manager.CategoryFunc = categoryFunc;
			}
			
			// Disconnect the old manager
			oldManager.CurrentPageChanged -= OnCurrentPageChanged;
			oldManager.PageAdded -= OnPageAdded;
			oldManager.PageRemoved -= OnPageRemoved;

			// Move the pages to the new manager
			Page current = oldManager.Current;
			while (oldManager.Count > 0)
			{
				Page page = oldManager[0];

				oldManager.Remove(page);
				pageManager.Add(page);
			}
			if (current != null)
			{
				pageManager.Present(current);
			}

			// Connect the new manager
			pageManager.CurrentPageChanged += OnCurrentPageChanged;
			pageManager.PageAdded += OnPageAdded;
			pageManager.PageRemoved += OnPageRemoved;

			oldManager.Destroy();
		}

		protected virtual void OnCurrentPageChanged(object sender, PageEventArgs args)
		{
			if (CurrentPageChanged != null)
			{
				CurrentPageChanged(this, args);
			}
		}

		protected virtual void OnPageAdded(object sender, PageEventArgs args)
		{
			if (PageAdded != null)
			{
				PageAdded(this, args);
			}
		}

		protected virtual void OnPageRemoved(object sender, PageEventArgs args)
		{
			if (PageRemoved != null)
			{
				PageRemoved(this, args);
			}
		}

		#endregion
	}
}
